<?php
echo $this->headScript()->setFile($this->baseUrl().'/js/jquery/table-sorter/jquery.dataTables.js');

function isnot_int($property_owner)
{
	$r = true;			
	if(is_numeric($property_owner))
	{ 
		if(is_int((int)$property_owner))
		{
			$r = false;
		}				
	}
	return $r;
}

function property_truncate($phrase,$start_words, $max_words)
{
   $phrase_array = explode(' ',$phrase);
   if(count($phrase_array) > $max_words && $max_words > 0)
	  $phrase = implode(' ',array_slice($phrase_array, $start_words, $max_words)).'...';  
   return $phrase;
}


$property_db = new Property_Model_DbTable_Properties();
$cat_Info = new Property_Model_DbTable_Category();
$mem_db = new Members_Model_DbTable_MemberList();
$group_db	= new Property_Model_DbTable_PropertyGroup();
$business_type_db = new Property_Model_DbTable_BusinessType();
$global_conf = Zend_Registry::get('global_conf');
$currency = new Zend_Currency($global_conf['default_locale']);
$auth = Zend_Auth::getInstance ();
?>
<script type="text/javascript">

$(document).ready(function() {
	
	/****************************Sorting Table Start*********************/
	var oTable = $("#sortTable").dataTable({
				"bPaginate": false,
				"bLengthChange": true,
				"bSearchable": true,
				"bFilter": true,
				"bSort": true,
				"bInfo": false,
				"bAutoWidth": true,
				"bProcessing": false,
				"bSortClasses": true,
				"bStateSave": false,
				"bServerSide": false,
				"aaSorting": [[ 1, "desc" ]],				
				"aoColumnDefs": [ 
						{ "bSortable": false, "bSortClasses": false,  "aTargets": [ 0 ] }
					],
				"oLanguage" : { "sSearch": "<?php echo $this->translator->translator('jquery_filter'); ?>" }
			}); 	
	
	$('td', oTable.fnGetNodes()).hover( function() {		
		$(this).parents("tr:first").children('td').addClass( 'highlighted' );
	}, function() {
		$('td.highlighted').removeClass('highlighted');
	});	
	
	/****************************Sorting Table End*********************/
	
		function stripslashes(str) 
		{
			str=str.replace(/\\'/g,'\'');
			str=str.replace(/\\"/g,'"');
			str=str.replace(/\\0/g,'\0');
			str=str.replace(/\\\\/g,'\\');
			return str;
		}
		
	
		allFunction();
	
			
		
		function deleteAll(self)
		{
			var id_str = '';
			for(var i=0;i < $('input.check_btn:checked').length;i++)
			{
				if(i == 0)
				{
					 id_str += $('input.check_btn:checked:eq('+i+')').val();
				}
				else
				{
					id_str += ','+$('input.check_btn:checked:eq('+i+')').val();
				}
			}
			
			$.ajax({
					url: "<?php echo $this->url( array('module'=> 'Property', 'controller' => 'backendpro', 'action'     => 'deletesavedall' ), 'adminrout',    true) ?>",
				 	type: 'POST',
				 	data: { id_st: id_str},
					beforeSend: function(){
						$(self).html('<span class="icon loader"></span>');
					 },
					success: function(response) 
				 	{
						//alert(response);
						var json_arr = eval("("+response+")");
						if(json_arr.status == 'ok')
						{
							for(var i= ($('input.check_btn:checked').length-1);i >= 0;i--)
							{								
								$('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").addClass('row_selected');							
							}
							var oTabled = $('#sortTable').dataTable();
							var anSelected = fnGetSelected( oTabled );
							for(var k = 0; k < anSelected.length; k++)
							{
								oTabled.fnDeleteRow( anSelected[k] );
							}
						}
						else
						{							
							openMsgDialog(json_arr.msg);
						}	
						$(self).html('<span class="icon delete"><?php echo html_entity_decode($this->translator->translator("common_delete_selected"), ENT_QUOTES, "UTF-8"); ?></span>');
					},
					error: function(xhr, status, error){
						var msg = "Error! " + xhr.status + " " + error;
						openMsgDialog(msg);
					}
			});	
		}
				
	$('a.delete_all').click(function() {
		var self = this;
		if($('input.check_btn:checked').length > 0)
		{
			$("#dialog-confirm").html("<?php echo addslashes($this->translator->translator('property_list_delete_confirm')); ?>").dialog({
					resizable: false,
					width:400,
					height:170,
					modal: true,
					show: 'fade',
					hide: 'explode',
					buttons: {
							'<?php echo html_entity_decode($this->translator->translator("common_delete_selected"), ENT_QUOTES, "UTF-8"); ?>': function() {
							deleteAll(self);
							$(this).dialog('close');
						},
					'<?php echo html_entity_decode($this->translator->translator("common_cancel"), ENT_QUOTES, "UTF-8"); ?>': function() {
							$(this).dialog('close');
						}
					}
				});		
		}
		else
		{
			var msg = "<?php echo $this->translator->translator('property_selected_err'); ?>";
			openMsgDialog(msg);
		}
	});
	
	$('input.checkAll_btn').click(function() {
		var self = this;
		if($(self).is(':checked'))
		{		
			$('input.check_btn').attr('checked', true);
		}
		else
		{
			$('input.check_btn').attr('checked', false);
		}
	});
			
});

function openMsgDialog(msg)
{
	$('#dialog_container').html(msg).dialog({
				autoOpen: true,
				modal: false,
				show: 'fade',
				resizable: 'false',				
				buttons: { "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, 'UTF-8'); ?>": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
				open: function(event, ui)
					 {
						setTimeout(function() 
						{									
							if($('#' + event.target.id).dialog('isOpen'))
							{
								$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
							}
						}, 10000);
					  }
			});
}

function deleteThis(self)
{
	var arr = $(self).attr('rel').split('_');
	$.ajax({
		 url: "<?php echo $this->url( array('module'=> 'Property', 'controller' => 'backendpro', 'action'     => 'deletesaved' ), 'adminrout',    true) ?>",
		 type: 'POST',
		 data: { id: arr[0]},
		 beforeSend: function(){
				$(self).html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" height="16" border="0" />');
			 },
		 success: function(response) 
		 {
			//alert(response);
			var json_arr = eval("("+response+")");
			if(json_arr.status == 'ok')
			{
				$(self).parents("li:first").parents("ul:first").parents("td:first").parents("tr:first").addClass('row_selected');
				var oTables = $('#sortTable').dataTable();
				var anSelected = fnGetSelected( oTables );
				oTables.fnDeleteRow( anSelected[0] );
			}
			else
			{
				openMsgDialog(json_arr.msg);
				$(self).html('<img src="application/modules/Administrator/layouts/scripts/images/tools/delete.png" title="<?php echo $this->translator->translator("common_delete_title"); ?>" border="0" />');
			}
		 },
		error: function(xhr, status, error){
			var msg = "Error! " + xhr.status + " " + error;
			openMsgDialog(msg);
		}
	});
}

/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
	var aReturn = new Array();
	var aTrs = oTableLocal.fnGetNodes();
	
	for ( var i=0 ; i<aTrs.length ; i++ )
	{
		if ( $(aTrs[i]).hasClass('row_selected') )
		{
			aReturn.push( aTrs[i] );
		}
	}
	return aReturn;
}

function allFunction()
{
	/************************************************IMAGE THUMBNIL************************************************/
	$('span.thumb').unbind('hover');
	$("span.thumb").hover(function() {
		$(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/ 
		$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
			.animate({
				marginTop: '-110px', /* The next 4 lines will vertically align this image */ 
				marginLeft: '-110px',
				top: '50%',
				left: '50%',
				width: '174px', /* Set new width */
				height: '174px', /* Set new height */
				padding: '20px'
			}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
	
		} , function() {
		$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
		$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
			.animate({
				marginTop: '-4px', /* Set alignment back to default */
				marginLeft: '0',
				top: '0',
				left: '0',
				width: '30px', /* Set width back to default */
				height: '20px', /* Set height back to default */
				padding: '5px'
			}, 400);
	});
	/************************************************IMAGE THUMBNIL END************************************************/
	
	$('a.delete_btn').unbind('click');
	$('a.delete_btn').click(function() {
		var self = this;
		$("#dialog-confirm").html("<?php echo addslashes($this->translator->translator('property_list_delete_confirm')); ?>").dialog({
			resizable: false,
			width:400,
			height:170,
			modal: true,
			show: 'fade',
			hide: 'explode',
			buttons: {
					'<?php echo html_entity_decode($this->translator->translator("property_delete"), ENT_QUOTES, "UTF-8"); ?>': function() {
					deleteThis(self);
					$(this).dialog('close');
				},
			'<?php echo html_entity_decode($this->translator->translator("common_cancel"), ENT_QUOTES, "UTF-8"); ?>': function() {
					$(this).dialog('close');
				}
			}
		});
		
	});
}
</script>

<div id="container">
<?php if (!$this->allow('saved')) : ?>
	<div class="grid_12 t_a_bottom">
		<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
	</div>
<?php else : ?>

<div class="title-container">
	<div class="property-saved"><h1><?php echo $this->translator->translator('property_page_list_saved_name'); ?></h1></div>
    
    <div class="button-container">
        <?php if ($this->allow('delete','backendpro','Property')) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" class="delete_all"><span class="icon delete"><?php echo html_entity_decode($this->translator->translator("common_delete_selected"), ENT_QUOTES, "UTF-8"); ?></span></a></span>
        <?php endif; ?>
    </div>
</div>

<div class="content-container">
	<div id="sidebar-inner">
    	<?php echo $this->partial('nav_menu.phtml');  ?>
    </div>
    
    <div id="content-inner">
    	<div id="topPagination" class="pagination">
        	<?php echo $this->paginationControl($this->datas, 'Sliding', 'top_pagination.phtml'); ?>
        </div>
        
        <div class="chart">
			<div class="data-table">
				<table id="sortTable" width="100%" align="center" cellpadding="0" cellspacing="0">
                	<thead>
                		<tr>
                            <th width="3%" class="center"><input type="checkbox" name="checkAll" id="checkAll" class="checkAll_btn" /></th>
                            <th width="5%"><?php echo $this->translator->translator('property_page_list_page_id'); ?></th>
                            <th width="16%"><?php echo $this->translator->translator('property_page_list_page_cat'); ?> </th>
                            <th width="6%"><?php echo $this->translator->translator('property_page_list_page_thumb'); ?> </th>
                            <th width="29%"><?php echo $this->translator->translator('property_page_list_page_pro_name'); ?> </th>
                            <th width="21%"><?php echo $this->translator->translator('property_page_list_who_saves'); ?> </th>						 
                            <th width="8%"><?php echo $this->translator->translator('common_action'); ?></th>
                		</tr>
                	</thead>
                    <tbody>
						<?php												
                        $area_db = new Property_Model_DbTable_Area();
                        $j = 1;
                        foreach ($this->datas as $entry):
                            if($j % 2 == 0)
                            {
                                $onmouseout = "this.bgColor='#FFFFFF';";
                                $background = '#FFFFFF';
                            }
                            else
                            {
                                $onmouseout = "this.bgColor='#F7F7F7';";
                                $background = '#F7F7F7';
                            }
                            $property_info = $property_db->getPropertiesInfo($this->escape($entry->property_id));	
                        ?>
                    	<tr bgcolor="<?php echo $background; ?>" onmouseover="this.bgColor='#FCF5DD';" onmouseout="<?php echo $onmouseout; ?>">
                     		<td class="center checkbox"><input type="checkbox" name="checkbtn" id="checkbtn" value="<?php echo $this->escape($entry->id); ?>" class="check_btn" /></td>
                      		<td class="center"><?php echo $this->escape($entry->id); ?></td>
                      		<td>
							<?php
                                if(empty($property_info['category_id']))
                                {
                                    echo "No Category";
                                }
                                else
                                {											  					 
                                        $category_info = $cat_Info->getCategoryInfo($this->escape($property_info['category_id']));
                                        echo $this->escape($category_info['category_name']);
                                }															
                      		?>
                      		</td>
                      		<td class="center"><span class="thumb"><?php $img_arr = ($property_info['property_primary_image'])? explode(',',$property_info['property_primary_image']) : explode(',',$property_info['property_image']); ?><img src="data/frontImages/property/property_image/<?php echo $this->escape($img_arr[0]); ?>" width="30" height="20" /></span></td>
                      		<td>
                      			<a href="<?php echo 'Property-details/'.$this->escape($property_info['property_title']); ?>" target="_blank" ><?php  echo $this->escape($property_info['property_name']); ?></a>
                       		</td>
                        	<td>
                        	<?php
                            	$mem_info = $mem_db->getMemberInfo($this->escape($entry->user_id));
                            	echo $this->escape($mem_info['username']);
                        	?>
                        	</td>						  
                      		<td class="tools-list">
								<ul>
									<li>
									<?php if ($this->allow('deletesaved','backendpro','Property')) : ?>
									<a href="javascript:void(0);" class="delete_btn"  rel="<?php echo $this->escape($entry->id); ?> ?>"><img src="application/modules/Administrator/layouts/scripts/images/tools/delete.png" title="<?php echo $this->translator->translator('common_delete_title'); ?>" border="0" /></a>
									<?php endif; ?>
									</li>
								</ul>
                      		</td>
                    	</tr>
                    	<?php 													
                        	$j++;
                        	endforeach;	
                      ?>
                   </tbody>
				</table>
            </div>
		</div>
        
        <div id="bottomPagination" class="pagination">
        	<?php echo $this->paginationControl($this->datas, 'Sliding', 'pagination.phtml'); ?>
        </div>        
	</div>
</div>
<?php endif; ?>

<div id="dialog_container" title="<?php echo $this->translator->translator('common_dialog_msg',$this->translator->translator('property_page_list_saved_name')); ?>"></div>
<div id="dialog-confirm" title="<?php echo $this->translator->translator('common_confirmation',$this->translator->translator('common_delete')); ?>"></div>
</div>	